/**
 * Project Name:dsrs-systemservice-service
 * File Name:UserGroupServiceImpl.java
 * Package Name:com.bwda.dsrs.systemservice.service.impl;
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.systemservice.service.impl;

import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.usergroup.UserGroupCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.po.usergroup.UserGroupPo;
import com.bwda.dsrs.systemservice.mapper.UserGroupMapper;
import com.bwda.dsrs.systemservice.service.UserGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户-自定义组关联关系
 *
 * @author liangjianyong
 * @date 2019/4/13
 */
@Service
public class UserGroupServiceImpl extends AbstractBaseServiceImpl<UserGroupPo, UserGroupCondition, UserGroupMapper>
    implements UserGroupService {
    
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void insert(UserGroupPo po) {
        super.insert(po);
    }
    
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int update(UserGroupPo po) {
        return super.update(po);
    }
    
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateWithValid(UserGroupPo po)
        throws SystemServiceException {
        super.updateWithValid(po);
    }
    
    @Override
    public List<UserGroupPo> queryList(UserGroupCondition condition) {
        return super.queryList(condition);
    }
    
    @Override
    public int queryCount(UserGroupCondition condition) {
        return super.queryCount(condition);
    }
    
    /**
     * 根据组code查询用户code列表
     * 
     * @param
     */
    @Override
    public List<String> queryUserCodeListByGroupCode(Integer groupId) {
        UserGroupCondition condition = new UserGroupCondition();
        condition.setGroupId(groupId);
        this.getMapper().selectList(condition);
        return null;
    }

    /**
     * 批量删除
     *
     * @param idList
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void batchDelete(List<Integer> idList) {
        UserGroupPo po = new UserGroupPo();
        po.setIdList(idList);
        this.getMapper().delete(po);
    }
    
    /**
     * 修改之后的userCode列表(会根据当前自定义组查询对应的id和参数比较做增删)
     * 
     * @param groupId
     * @param userCodeList
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateUserGroup(Integer groupId, List<String> userCodeList) {
        //查询自定义组当前对应的记录
        UserGroupCondition queryCondition=new UserGroupCondition();
        queryCondition.setGroupId(groupId);
        List<UserGroupPo> poList=this.queryList(queryCondition);
        //当前id集合
        List<String> oldUserCodeSlist=new ArrayList<>();
        if(poList!=null&&poList.size()>0){
            oldUserCodeSlist=poList.stream().map(UserGroupPo::getUserCode).collect(Collectors.toList());
        }
        //删除此次修改剔除的用户关联
        if(oldUserCodeSlist!=null&&oldUserCodeSlist.size()>0){
            UserGroupCondition delCondition=new UserGroupCondition();
            delCondition.setGroupId(groupId);
            delCondition.setUsreCodeListInFlag(false);
            delCondition.setUserCodeList(userCodeList);
            delCondition.setDelFlag(Constants.DELETED);
            this.getMapper().updateByGroupAndUserCodeList(delCondition);
        }
        if(userCodeList==null){
            userCodeList=new ArrayList<>();
        }
        //要新增的userCode集合
        List<String> addList=new ArrayList<>();
        for(String tempUserCode:userCodeList){
            if(!oldUserCodeSlist.contains(tempUserCode)){
                addList.add(tempUserCode);
            }
        }
        //循环添加（ID问题）
        for(String userCode:addList){
            UserGroupPo po=new UserGroupPo();
            po.setGroupId(groupId);
            po.setUserCode(userCode);
            this.insert(po);
        }

    }
}